//第三层
const mongoose = require('mongoose')
const merchantModel = mongoose.model('merchantModel')
const shopModel = mongoose.model('shopModel')
const adminModel = mongoose.model('adminModel')
//获取所有商家数据
module.exports.pAdmin = async function ({ pageNum, currentPage }) {
    let total = (await merchantModel.find()).length
    let rows = await merchantModel.find()
        .limit(pageNum - 0)
        .skip((currentPage - 1) * pageNum)
        
    return { total, rows }
}
//根据条件查询商家数据
module.exports.pAdminSearch = async function ({ pageNum, currentPage,selectType,inputValue}) {
    console.info(await merchantModel.find())
    let total = (await merchantModel.find({[selectType]:{$regex: inputValue, $options: '$i' }})).length
    let rows = await merchantModel.find({[selectType]:{$regex: inputValue, $options: '$i' }})
        .limit(pageNum - 0)
        .skip((currentPage - 1) * pageNum)
    return { total, rows }
}
//根据_id获取商家数据
module.exports.pAdminId = async function (_id) {
    console.info(_id)
    const data = await merchantModel.find(_id).populate('shopId')
    return data
}
//得到已冻结的商家数据
module.exports.pAdminUpdate = async function ({ _id, ...all }) {
    return await merchantModel.updateOne({ _id }, { ...all })
}

//获取所有店铺数据
module.exports.shopList = async function ({ pageNum, currentPage }) {
    let total = (await shopModel.find()).length
    let rows = await shopModel.find()
        .limit(pageNum - 0)
        .skip((currentPage - 1) * pageNum)
    return { total, rows }
}

//根据查询条件获取店铺数据
module.exports.shopListSearch = async function ({ pageNum, currentPage,selectType,inputValue}) {
    let total = (await shopModel.find({[selectType]:{$regex: inputValue, $options: '$i' }})).length
    let rows = await shopModel.find({[selectType]:{$regex: inputValue, $options: '$i' }})
        .limit(pageNum - 0)
        .skip((currentPage - 1) * pageNum)
    return { total, rows }
}

//根据_id获取店铺数据
module.exports.shopId = async function (_id) {
    const data = await shopModel.find(_id).populate('merchantId')
    return data

}

//得到已冻结的店铺数据
module.exports.pAdminUpdateShop = async function ({ _id, ...all }) {
    return await shopModel.updateOne({ _id }, { ...all }).populate('merchantId')
}

//平台管理员登录
module.exports.loginer = async function (sj) {
    const data = await adminModel.find(sj)
    return data

}

//管理员登录
//商家登录
module.exports.login3 = async (data) => {
    // console.log(data)
    return await mongoose.model('merchantModel').find(data);
}

//商家注册
module.exports.Add3 = async (data) => {
    return await mongoose.model('merchantModel').create(data.params);
}